package com.gao.utils;

import com.gao.constants.LoginConstants;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Random;

/**
 * @author DarkRepulser
 * @date 2021/11/1 9:04
 * @desc 验证码相关工具类
 */
public class CodeUtils {



    /**
     * 随机生成验证码串
     * @param length
     * @return
     */
    public static String getRandomVerifyCode(int length) {
        String val = "";
        Random random = new Random();

        // 参数length，表示生成几位随机数
        for (int i = 0; i < length; i++) {

            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    public static String getImgVerifyCode(String code) throws IOException {
//        内存中生成图像
        BufferedImage image = new BufferedImage(LoginConstants.WIDTH,LoginConstants.HEIGHT,BufferedImage.TYPE_INT_RGB);
        final Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0,0,LoginConstants.WIDTH,LoginConstants.HEIGHT);
        graphics.drawRect(0,0,LoginConstants.WIDTH - 1,LoginConstants.HEIGHT - 1);
        final Random random = new Random();
        for(int i = 0;i < LoginConstants.COUNT;i++){
            graphics.setColor(getRandColor(150,200));
            final int x = random.nextInt(LoginConstants.WIDTH - LoginConstants.LINEWIDTH - 1) + 1;
            final int y = random.nextInt(LoginConstants.HEIGHT - LoginConstants.LINEWIDTH - 1) + 1;
            final int xl = random.nextInt(LoginConstants.LINEWIDTH);
            final int yl = random.nextInt(LoginConstants.LINEWIDTH);
            graphics.drawLine(x,y,x + xl,y + yl);
        }

        for(int i = 0;i < code.length();i++){
            graphics.setColor(Color.BLACK);
            graphics.setFont(new Font("Times New Roman",Font.BOLD,24));
            graphics.drawString(String.valueOf(code.charAt(i)),(23 * i) + 8, 26);
        }
        graphics.dispose();

        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ImageIO.write(image,LoginConstants.IMG_FORMAT,byteStream);
        byte[] bytes = byteStream.toByteArray();
        String base64 = Base64.getEncoder().encodeToString(bytes).trim();
        base64 = base64.replaceAll("\n","").replaceAll("\n","");

        return LoginConstants.BASE64_PRE + base64;
    }

    private static Color getRandColor(int fc,int bc){
        final Random random = new Random();
        if (fc > 255)
            fc = 255;
        if (bc > 255)
            bc = 255;

        final int r = fc + random.nextInt(bc - fc);
        final int g = fc + random.nextInt(bc - fc);
        final int b = fc + random.nextInt(bc - fc);

        return new Color(r,g,b);
    }

}
